<template>
  <div class="app-container">
    <div class="funOperate">
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item label="学生姓名:" prop="sdudentName">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入学生姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="租赁时间:">
          <el-date-picker
            v-model="dateRange"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd"
            >新增</el-button
          >
        </el-col>
      </el-row>
    </div>

    <el-table
      v-loading="loading"
      :data="rentList"
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租赁ID" align="center" prop="id" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="车辆类型" align="center" prop="bikeType" />
      <el-table-column label="车辆编号" align="center" prop="bikeNumber" />
      <el-table-column label="租借日期" align="center" prop="rentTime" />
      <el-table-column label="归还日期" align="center" prop="returnTime" />
      <el-table-column label="归还状态" align="center" prop="returnType">
        <template slot-scope="scope">
          {{ scope.row.returnType == 0 ? "未归还" : "已归还" }}
        </template>
      </el-table-column>
      <el-table-column
        label="押金"
        align="center"
        width="60"
        prop="antecedentMoney"
      >
        <template slot-scope="scope">
          {{
            scope.row.antecedentMoney == "" || scope.row.antecedentMoney == null
              ? ""
              : "¥" + scope.row.antecedentMoney
          }}
        </template>
      </el-table-column>
      <el-table-column label="租赁金额" align="center" prop="rentAmount">
        <template slot-scope="scope">
          {{
            scope.row.rentAmount == "" || scope.row.rentAmount == null
              ? ""
              : "¥" + scope.row.rentAmount
          }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="comment" />
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-lock"
            @click="returnBike(scope.row)"
          >
            归还
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />
    <!-- 添加或修改租赁信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="学生名称" prop="useruame">
          <el-select
            v-model="form.studentName"
            placeholder="请选择学生名称"
            filterable
            allow-create
            default-first-option
          >
            <el-option
              v-for="item in studentList"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自行车">
          <el-cascader
            :options="rentTree"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="押金(元)" prop="antecedentMoney">
          <el-input v-model="form.antecedentMoney" placeholder="请输入押金" />
        </el-form-item>
        <el-form-item label="描述" prop="comment">
          <el-input
            type="textarea"
            v-model="form.comment"
            placeholder="请输入描述内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 归还单车信息对话框 -->
    <el-dialog
      title="归还单车"
      :visible.sync="returnOpen"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生名称" prop="studentName">
          <el-input
            v-model="form.studentName"
            placeholder="请输入学生名称"
            disabled
          />
        </el-form-item>
        <el-form-item label="车辆类型" prop="bikeType">
          <el-input
            v-model="form.bikeType"
            placeholder="请输入车辆类型"
            disabled
          />
        </el-form-item>
        <el-form-item label="车辆编号" prop="bikeNumber">
          <el-input
            v-model="form.bikeNumber"
            placeholder="请输入车辆编号"
            disabled
          />
        </el-form-item>
        <el-form-item label="租借日期" prop="rentTime">
          <el-input
            v-model="form.rentTime"
            placeholder="请输入租借日期"
            disabled
          />
        </el-form-item>
        <el-form-item label="押金" prop="antecedentMoney">
          <el-input
            v-model="form.antecedentMoney"
            placeholder="请输入押金"
            disabled
          />
        </el-form-item>

        <el-form-item label="租赁金额" prop="rentAmount">
          <el-input v-model="form.rentAmount" placeholder="请输入租赁金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReturnBike">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRent,
  getRent,
  addRent,
  rentTree,
  updateRent,
  returnBike,
  deleteRent,
} from "@/api/rent";
import { listStudent } from "@/api/student";
export default {
  name: "Rent",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 租赁表表格数据
      rentList: [],
      // 所有学生信息
      studentList: [],
      // 自行车树
      rentTree: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 归还弹出层
      returnOpen: false,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        name: null,
        startTime: null,
        endTime: null,
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getStudentList();
    this.rentTreeList();
  },
  methods: {
    //归还单车
    submitReturnBike(){
      returnBike(this.form).then((response) => {
          this.$message({
            message: "归还成功",
            type: "success",
          });
          this.returnOpen = false;
          this.getList();
        });
    },
    // 查询自行车树
    rentTreeList() {
      rentTree().then((response) => {
        this.rentTree = response.data;
      });
    },
    // 查询所有学生信息
    getStudentList() {
      listStudent({
        page: 1,
        limit: 9999,
      }).then((response) => {
        this.studentList = response.data;
      });
    },
    // 查询租赁表列表
    getList() {
      this.loading = true;
      if (this.dateRange) {
        this.queryParams.startTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
      listRent(this.queryParams).then((response) => {
        this.rentList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.dateRange = [];
      this.queryParams.name = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 新增按钮操作
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租赁信息";
    },
    // 修改按钮操作
    handleUpdate(row) {
      this.reset();
      getRent(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租赁信息";
      });
    },
    // 归还单车
    returnBike(row) {
      this.reset();
      getRent(row.id).then((response) => {
        this.form = response.data;
        this.returnOpen = true;
      });
    },
    // 提交按钮
    submitForm() {
      if (this.form.id != null) {
        updateRent(this.form).then((response) => {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.open = false;
          this.getList();
        });
      } else {
        addRent(this.form).then((response) => {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.open = false;
          this.getList();
        });
      }
    },
    // 删除按钮操作
    handleDelete(row) {
      const id = row.id;
      this.$confirm(
        '是否确认删除租赁信息编号为"' + id + '"的数据项吗？',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          return deleteRent(id);
        })
        .then(() => {
          this.getList();
          this.$message({
            message: "删除成功",
            type: "success",
          });
        })
        .catch(() => {});
    },
    handleChange(value) {
      this.form.bikeType = value[0];
      this.form.bikeNumber = value[1];
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  padding: 20px;
}
.funOperate {
  display: flex;
  justify-content: space-between;
}
</style>
